package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entry.Employee;
import com.itheima.pojo.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    @PostMapping("/login")
    //返回值为R<Employee>类型封装了Employee实体类和msg还有code，因为要存储session所以要参数request，因为前段返回的是json格式的数据
    //所以需要加注解@RequestBody   把json数据封装成一个Employee类
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();//获取密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());//密码同md5方式加密
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();//创建条件构造器
        wrapper.eq(Employee::getUsername, employee.getUsername());//通过.eq方法来添加条件
        Employee one = service.getOne(wrapper);//select * from reggie where username=?;
        //判断通过getOne方法查询出来的是否为空
        if (one == null) {
            return R.error("用户名或密码错误");
        }
        //判断账号对应的密码是否正确
        if (!one.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }
        //判断数据的statues属性是否为0为0的话会冻结
        if (one.getStatus() == 0) {
            return R.error("账户已被冻结");
        }
        //将登陆成功的id信息存入session
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //销毁session
        request.getSession().invalidate();
        return R.success("退出成功");
    }

    // INSERT into employee (id,NAME,username,PASSWORD,phone,sex,id_number,STATUS) VALUES(?,?,?,?,?,?,?,?);
    @PostMapping
    public R<Object> add(HttpServletRequest request, @RequestBody Employee employee) {
   /*     Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(employee1);

        employee.setUpdateUser(employee1);
        employee.setCreateUser(employee1);*/
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        service.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Map> findPage(Integer page, Integer pageSize, String name) {
        Page<Employee> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        wrapper.orderByDesc(Employee::getCreateTime);

        Page<Employee> page2 = service.page(page1, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page2.getTotal());
        map.put("records", page2.getRecords());
        return R.success(map);
    }

    @PutMapping
    public R<String> upDateStatus(HttpServletRequest request, @RequestBody Employee employee) {
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(employee1);
        service.updateById(employee);
        return R.success("完成");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {

        Employee byId = service.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("没有查询到员工信息");
    }

}

